
#include "/node_modules/lygia/math/map.glsl"
#include "/node_modules/lygia/generative/random.glsl"
#include "/node_modules/lygia/generative/snoise.glsl"

uniform float u_time;
uniform float u_speedFactor;
varying vec2 v_uv;

// 获得随机颜色
vec3 getColor(vec2 uv) {
    uv += vec2(9., 0.);
    float r = random(uv + vec2(12., 2.));
    float g = random(uv + vec2(7., 5.));
    float b = random(uv);
    vec3 col = vec3(r, g, b);
    return col;
}

// 颜色噪声
vec3 colorNoise(vec2 uv) {
    vec2 newUV = floor(uv);
    vec2 size = vec2(1.);
    vec3 v1 = getColor((newUV + vec2(0.)) / size);
    vec3 v2 = getColor((newUV + vec2(0., 1.)) / size);
    vec3 v3 = getColor((newUV + vec2(1., 0.)) / size);
    vec3 v4 = getColor((newUV + vec2(1.)) / size);
    vec2 factor = smoothstep(0., 1., fract(uv));
    vec3 v1Tov2 = mix(v1, v2, factor.y);
    vec3 v3Tov4 = mix(v3, v4, factor.y);
    vec3 mixColor = mix(v1Tov2, v3Tov4, factor.x);
    return mixColor;
}

void main() {
    vec2 new_uv = -v_uv;
    // x轴随时间变化
    new_uv.x += u_time * .5;

    // 颜色噪声
    vec3 color = colorNoise(new_uv * vec2(10., 100.));

    // 颜色变化 根据噪声和一个固定颜色得到新颜色
    color *= vec3(1.5, 1., 400.);

    // 透明度进行噪声
    float alpha = snoise(new_uv * vec2(3., 100.));
    alpha = map(alpha, -1., 1., 0., 1.);
    alpha = pow(clamp(alpha - .05, 0., 1.), 30.);

    // 取值  0~1
    alpha = smoothstep(0., .04, alpha);

    // x轴两边一样
    alpha *= smoothstep(.02, .5, v_uv.x) * smoothstep(.02, .5, 1. - v_uv.x);
    alpha *= smoothstep(.01, .1, v_uv.y) * smoothstep(.01, .1, 1. - v_uv.y);

    alpha *= smoothstep(0., 1., u_speedFactor) * 5.;

    csm_FragColor = vec4(color, alpha);
}